package mamikeji.liaowa.ui.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.umeng.analytics.MobclickAgent;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.application.MyApplication;
import mamikeji.liaowa.bean.FirstBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;

/**
 * Created by 廉冲-android开发 on 2017/5/5.
 */
public class FistActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "FistActivity";
    private TextView textView_first_login, textView_first_welcome_msg;
    private RelativeLayout relativeLayout_fist_justadd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fist);
        getBase();
        tileBar(this, false);
        getLogin();
        init();
    }

    private void getLogin(){
        if (!ForPreference.getString(ForContents.USER_PHONE).equals("") && ForPreference.getString(ForContents.USER_PHONE) != null) {
            //其次判断用户是否登录0
            //如果已登录，判断用户娃数量，少于一个，跳转添加娃页面，如果已经有娃，跳转到首页
            if (ForPreference.getInt(ForContents.USER_COUNT) <= 0) {
                ForToast.showToast(R.string.fist_wa_null);
                startActivity(new Intent(FistActivity.this, AddWaActivity.class));
            } else {
                startActivity(new Intent(FistActivity.this, MainActivity.class));
                finish();
            }
        }
    }

    /**
     * 加载布局控件
     */
    private void init() {
        textView_first_login = (TextView) findViewById(R.id.textView_first_login);
        textView_first_welcome_msg = (TextView) findViewById(R.id.textView_first_welcome_msg);
        relativeLayout_fist_justadd = (RelativeLayout) findViewById(R.id.relativeLayout_fist_justadd);

         /*
         * 必须事先在assets底下创建一fonts文件夹 并放入要使用的字体文件(.ttf)
         * 并提供相对路径给creatFromAsset()来创建Typeface对象
         */
        Typeface fontFace = Typeface.createFromAsset(getAssets(),
                "fonts/main_text_css.ttf");
        // 字体文件必须是true type font的格式(ttf)；
        // 当使用外部字体却又发现字体没有变化的时候(以 Droid Sans代替)，通常是因为
        // 这个字体android没有支持,而非你的程序发生了错误
        textView_first_welcome_msg.setTypeface(fontFace);

        textView_first_login.setOnClickListener(this);
        relativeLayout_fist_justadd.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.relativeLayout_fist_justadd:
                showLoading();
                MobclickAgent.onEvent(this,"LandingJustLook");//初次登录-随便看看点击
                startActivity(new Intent(FistActivity.this, MainActivity.class));
                break;
            case R.id.textView_first_login:
                startActivity(new Intent(FistActivity.this, LoginActivity.class));
                break;
        }
    }

    /**
     * 立即加入
     */
    public void fistAdd() {
        showLoading();
        Map<String, String> formMap = new HashMap<>();
        formMap.put("id", ForContents.GET_2);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.FIRST_ADD, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage().toString());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        FirstBean firstBean = new Gson().fromJson(response, FirstBean.class);

                        //储存登陆信息，token
                        ForPreference.putString(ForContents.USER_ID, firstBean.getUser().getId() + "");
                        ForPreference.putString(ForContents.USER_CODE, firstBean.getUser().getUsercode());
                        ForPreference.putInt(ForContents.USER_COUNT, firstBean.getUser().getCount());

                        Intent mIntent;
                        if (ForPreference.getInt(ForContents.USER_COUNT) <= 0) {
                            //跳转添加娃
                            ForToast.showToast(R.string.fist_wa_null);
                            mIntent = new Intent(FistActivity.this, AddWaActivity.class);
                        } else {
                            //跳转主页面
                            mIntent = new Intent(FistActivity.this, MainActivity.class);
                        }
                        MyApplication.getInstance().closeActivity(new FistActivity());
                        startActivity(mIntent);
                        finish();
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
